package com.yyx.lgb.manager.service.impl;

import com.yyx.lgb.common.exception.LgbException;
import com.yyx.lgb.manager.Helper.MenuHelper;
import com.yyx.lgb.manager.mapper.SysMenuMapper;
import com.yyx.lgb.manager.mapper.SysRoleMapper;
import com.yyx.lgb.manager.mapper.SysRoleMenuMapper;
import com.yyx.lgb.manager.mapper.SysRoleUserMapper;
import com.yyx.lgb.manager.service.SysMenuService;
import com.yyx.lgb.model.entity.system.SysMenu;
import com.yyx.lgb.model.entity.system.SysUser;
import com.yyx.lgb.model.vo.common.ResultCodeEnum;
import com.yyx.lgb.model.vo.system.SysMenuVo;
import com.yyx.lgb.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * ClassName: SysMenuServiceImpl
 * Package: com.yyx.lgb.manager.service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/8/5 - 11:12
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> findNodes() {
       List<SysMenu> sysMenus= sysMenuMapper.selectAll();
       if(CollectionUtils.isEmpty(sysMenus))
           return null;
        ////2调用工具类的方法，把返回List集合封装要求数据格式
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenus); //构建树形数据
        return treeList;
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.insert(sysMenu);
        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开 1
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {

        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if(parentMenu != null) {

            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;

        }

    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.countByParentId(id);// 先查询是否存在子菜单，如果存在不允许进行删除
        //判断 count > 0  包含子菜单
        if (count > 0) {
            throw new LgbException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.removeById(id);
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        //获取用户ID
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        //根据userId查询可以操作的菜单
      List<SysMenu> sysMenuList=  sysMenuMapper.selectListByUserId(userId);
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);

        return this.buildMenus(sysMenuTreeList);
    }

    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
